from sqlalchemy import desc, and_
from model.user_collection import UserCollection


class UserCollectionDao(object):
    def __init__(self, session):
        """
        :param session:
        :type session:Session
        """
        self.session = session

    def add(self, coll):
        self.session.add(coll)

    def get_collection_by_user_id(self, d={}):
        user_id = d.get("user_id", "")
        q = self.session.query(UserCollection)
        if user_id:
            q = q.filter(UserCollection.user_id == user_id)
        q = q.order_by(desc(UserCollection.create_time)).all()
        return q

    def get_collection_by_job_uid(self, job_id, user_id):
        q = self.session.query(UserCollection)
        q = q.filter(and_(UserCollection.user_id == user_id,
                          UserCollection.job_id == job_id))
        return q.all()
